<?php 
/*
 module:		用户管理控制器
 create_time:	2022-08-09 17:31:50
 author:		
 contact:		
*/

namespace app\Controller\admin;
use function Hyperf\ViewEngine\view;
use App\Exception\ValidateException;
use App\Model\admin\AdminuserModel;
use App\Controller\admin\CommonController;
use Hyperf\DbConnection\Db;

class AdminuserController extends CommonController {


	/*
 	* @Description  数据列表
 	*/
	function index(){
		if($this->request->isMethod('get')){
			return view('admin/adminuser/index');
		}else{
			$limit  = $this->request->input('limit', 20);
			$page = $this->request->input('page', 1);

			$order  = $this->request->input('order');	//排序方式
			$sort  = $this->request->input('sort');		//排序字段

			$where = [];
			$where['user_id'] = $this->request->input('user_id');
			$where['name'] = $this->request->input('name');
			$where['user'] = $this->request->input('user');
			$where['role_id'] = $this->request->input('role_id');
			$where['status'] = $this->request->input('status');

			$sort = !empty($sort) ? $sort : 'user_id';
			$order = !empty($order) ? $order : 'desc';

			$with = [
				'role'=>function($query){
					$query->select(explode(',','role_id,name'));
				},
			];

			$field = 'user_id,name,user,note,status,create_time,role_id';

			$query = AdminuserModel::select(explode(",",$field));

			$res = formatWhere($query,$where)->with($with)->orderBy($sort,$order)->paginate($limit)->toArray();

			$data['status'] = 200;
			$data['data'] = $res;
			$page == 1 && $data['sql_field_data'] = $this->getSqlField('role_id');
			return json($data);
		}
	}


	/*
 	* @Description  修改排序开关
 	*/
	function updateExt(){
		$postField = 'user_id,status';
		$data = $this->request->inputs(explode(',',$postField),'post');
		if(!$data['user_id']) throw new ValidateException ('参数错误');
		AdminuserModel::whereIn('user_id',explode(',',$data['user_id']))->update($data);
		return json(['status'=>200,'msg'=>'操作成功']);
	}

	/*
 	* @Description  添加
 	*/
	public function add(){
		$postField = 'name,user,pwd,role_id,note,status,create_time';
		$data = $this->request->inputs(explode(',',$postField),'post');

		$validate = new \App\Validate\admin\AdminuserValidate;
		if(!$validate->scene('add')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		$data['pwd'] = md5($data['pwd'].config('my.password_secrect'));
		$data['create_time'] = time();

		try{
			$res = AdminuserModel::insertGetId($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
	}


	/*
 	* @Description  修改
 	*/
	public function update(){
		$postField = 'user_id,name,user,role_id,note,status,create_time';
		$data = $this->request->inputs(explode(',',$postField),'post');

		$validate = new \App\Validate\admin\AdminuserValidate;
		if(!$validate->scene('update')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		$data['create_time'] = strtotime($data['create_time']);

		try{
			AdminuserModel::where('user_id',$data['user_id'])->update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'msg'=>'修改成功']);
	}


	/*
 	* @Description  修改信息之前查询信息的 勿要删除
 	*/
	function getUpdateInfo(){
		$id =  $this->request->input('user_id');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'user_id,name,user,role_id,note,status,create_time';
		$res = AdminuserModel::select(explode(",",$field))->find($id);
		return json(['status'=>200,'data'=>$res]);
	}


	/*
 	* @Description  重置密码
 	*/
	public function resetPwd(){
		$postField = 'user_id,pwd';
		$data = $this->request->inputs(explode(',',$postField),'post');
		if(empty($data['user_id'])) throw new ValidateException ('参数错误');
		if(empty($data['pwd'])) throw new ValidateException ('密码不能为空');

		$data['pwd'] = md5($data['pwd'].config('my.password_secrect'));
		$res = AdminuserModel::where('user_id',$data['user_id'])->update($data);
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/*
 	* @Description  获取定义sql语句的字段信息
 	*/
	function getFieldList(){
		return json(['status'=>200,'data'=>$this->getSqlField('role_id')]);
	}

	/*
 	* @Description  获取定义sql语句的字段信息
 	*/
	private function getSqlField($list){
		$data = [];
		if(in_array('role_id',explode(',',$list))){
			$data['role_ids'] = $this->query('select role_id,name from pre_role','default');
		}
		return $data;
	}





}



